//
//  BN_LoginView.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-4-2.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_LoginView.h"

@implementation BN_LoginView {
    BN_UserInfo *userInfo_;
}

- (void)callBackLoginFinished:(LoginFinished)loginFinished {
    _loginFinished = loginFinished;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 帮助类
    userHelper_ = [[BN_UserHelper alloc]init];

    
    // 忘记密码
    UIColor *color = APP_OBJECT_GREEN_COLOR;
    self.forgetPasswordBtn.backgroundColor = color;
    self.forgetPasswordBtn.layer.cornerRadius = 14;
    self.forgetPasswordBtn.layer.masksToBounds = YES;
    [self.forgetPasswordBtn addTarget:self action:@selector(forgetPasswordAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 登录按钮
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 输入框
    if ([BN_UserHelper unArchiverAccountInfo]) {
        userInfo_ = [BN_UserHelper unArchiverAccountInfo];
    };
    self.userNameTextField.text = userInfo_.account;
    _userNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.userNameTextField.delegate = self;
    self.passwordTextField.delegate = self;
    self.userNameTextField.returnKeyType = UIReturnKeyNext;
    self.passwordTextField.returnKeyType = UIReturnKeyGo;
    [self.passwordTextField setSecureTextEntry:YES];
    if (self.userNameTextField.text.length > 0) {
        [self.passwordTextField becomeFirstResponder];
    }else {
        [self.userNameTextField becomeFirstResponder];
    }
    
    UILabel *username = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 43)];
    username.textColor = [UIColor grayColor];
    username.alpha = 0.5;
    username.font = [UIFont systemFontOfSize:17];
    username.text = @"  账 号  ";
    self.userNameTextField.leftView = username;
    self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    username.textAlignment = NSTextAlignmentCenter;
    
    UILabel *password = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 43)];
    password.textColor = [UIColor grayColor];
    password.alpha = 0.5;
    password.font = [UIFont systemFontOfSize:17];
    password.text = @"  密 码  ";
    password.textAlignment = NSTextAlignmentCenter;

    self.passwordTextField.leftView = password;
    self.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    self.passwordTextField.clearsOnBeginEditing = YES;
    // 取消弹出键盘
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard)];
    [self addGestureRecognizer:tapGest];
}

#pragma mark
#pragma mark - Action
- (void)hideKeyBoard {
    [self.userNameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
    [UIView animateWithDuration:0.3 animations:^{
        if (IOS7) {
            self.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
        }
        if (MAIN_VERSION < 7) {
            self.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
        }
    } completion:^(BOOL finished) {
        //
    }];
}

/**
 *  登录按钮触发的方法
 */
- (void)loginBtnAction {
    
    [self hideKeyBoard];

    NSString *userNameStr = self.userNameTextField.text;
    NSString *passwordStr = self.passwordTextField.text;
    
    // 允许登录
    if (userNameStr.length !=0 && passwordStr.length != 0) {
        [SVProgressHUD showWithStatus:@"登录中..." maskType:SVProgressHUDMaskTypeBlack];
        
        NSString *userName = nil;
        NSString *passWord = nil;
        userName = self.userNameTextField.text;
        passWord = self.passwordTextField.text;
        
        [userHelper_ loginWithAccount:userName password:passWord completeHandler:^(id result, NSString *errMsg) {
            if (errMsg) {
                [SVProgressHUD showErrorWithStatus:errMsg];
                _loginFinished(NO);
            }else{
                // 提示消失
                [SVProgressHUD dismiss];
                userInfo_ = [BN_UserHelper unArchiverAccountInfo];
                if(![[NSUserDefaults standardUserDefaults] boolForKey:FIRSTRUN]){
                    if (![[NSUserDefaults standardUserDefaults]integerForKey:USERDEFAULTS_CLASSPARENT]) {
                        [[NSUserDefaults standardUserDefaults]setInteger:0 forKey:USERDEFAULTS_CLASSPARENT];
                        [[NSUserDefaults standardUserDefaults]synchronize];
                    }
                    
                    _loginFinished(YES);
                }
            }
        }];
    }else {
        [SVProgressHUD showErrorWithStatus:@"请输入用户名或密码"];
    }
}

- (void)forgetPasswordAction {
    BN_ForgetPasswordView *forgetView = [[[NSBundle mainBundle]loadNibNamed:@"BN_ForgetPasswordView" owner:nil options:nil]objectAtIndex:0];
    [self addSubview:forgetView];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (SCREEN_HEIGHT <= 480) {
        if ([textField isEqual:self.passwordTextField]) {
            [UIView animateWithDuration:0.3 animations:^{
                self.frame = CGRectMake(0, -90, SCREEN_WIDTH, SCREEN_HEIGHT+216);
            } completion:^(BOOL finished) {
                //
            }];
        }
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField isEqual:self.userNameTextField]) {
        [self.passwordTextField becomeFirstResponder];
    }
    if ([textField isEqual:self.passwordTextField]) {
        [self loginBtnAction];
    }
    return YES;
}

#pragma mark dealloc
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KLOGINAPP object:nil];
}
@end
